﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Security.Cryptography;

namespace DAL
{
    public class DBController
    {
        
        private string ConnString;

        public DBController() {
            ConnString = ConfigurationSettings.AppSettings["main"];
        }

        /// <summary>
        /// /Funcion que abre la conexion a la base de datos
        /// </summary>
        /// <returns></returns>
        public SqlConnection OpenConn()
        {
            SqlConnection conn = new SqlConnection(ConnString);
            conn.Open();
            return conn;
        }

        /// <summary>
        /// Funcion que crea la conexion con la base de datos
        /// </summary>
        /// <param name="conn"></param>
        /// <returns></returns>
        public bool CloseConn(SqlConnection conn){
            try
            {
                conn.Close();
                conn.Dispose();
                return true;
            }catch(Exception ex){
                throw ex;
            }
        }


        /// <summary>
        /// Funcion que encripta un texto
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public string GetSHA256(string text)
        {
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] hashValue;
            byte[] message = UE.GetBytes(text);

            SHA256Managed hashString = new SHA256Managed();
            string hex = "";

            hashValue = hashString.ComputeHash(message);
            foreach (byte x in hashValue)
            {
                hex += String.Format("{0:x2}", x);
            }
            return hex;
        }
    }
}
